package com.julis.activity.record.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.github.czy1121.view.RoundButton;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import com.julis.R;
import com.julis.activity.record.Record_Details_Activity;
import com.julis.bean.Obu_Info;

/**
 * Created by juli on 2020/3/3.
 */

public class MyRVAdapter extends RecyclerView.Adapter<MyRVAdapter.MyTVHolder> {
    /**
     * 数据存储集合
     **/
    public List<Obu_Info> mDataList = new ArrayList<Obu_Info>();

    /**
     * 集合方式添加数据
     *
     * @param collection 集合
     */
    public boolean addItem(Collection<Obu_Info> collection) {
        boolean b = mDataList.addAll(collection);
        notifyDataSetChanged();
        return b;

    }

    private final LayoutInflater mLayoutInflater;
    private final Context mContext;
//    private final ArrayList<String> mData;

    public MyRVAdapter(Context context) {
        mLayoutInflater = LayoutInflater.from(context);
        mContext = context;


    }

    /**
     * 清空数据
     */
    public void clear() {
        mDataList.clear();
    }

    @Override
    public MyRVAdapter.MyTVHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyRVAdapter.MyTVHolder(mLayoutInflater.inflate(R.layout.tab_item_1_list, parent, false));
    }

    @Override
    public void onBindViewHolder(final MyRVAdapter.MyTVHolder holder, int pos) {
        Obu_Info obu_info = mDataList.get(pos);
        String getObu_input_success = mDataList.get(pos).getObu_input_success();
        String getVin_input_success = mDataList.get(pos).getVin_input_success();
        String getObu_check_success = mDataList.get(pos).getObu_check_success();
        String getBle_check_success = mDataList.get(pos).getBle_check_success();
        String getVin_write_success = mDataList.get(pos).getVin_write_success();
        if (getVin_input_success.equals("true")) {
            holder.Revin.setVisibility(View.VISIBLE);
        } else {
            holder.Revin.setVisibility(View.GONE);
        }

        holder.tv_chenggong.setText("检测完成");
        holder.tv_chenggong.setTextColor(mContext.getResources().getColor(R.color.ym_green));
        String obu_code = mDataList.get(pos).getObu_code();
        holder.tv_vin.setText("VIN码：" + mDataList.get(pos).getVincode());
        holder.tv_obu.setText("OBU MAC号：" + obu_code);
        holder.tv_time.setText("检测时间：" + mDataList.get(pos).getDevice_check_device_check_time());
        holder.roundButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String id = mDataList.get(pos).getId();
                mContext.startActivity(new Intent(mContext, Record_Details_Activity.class).putExtra("uuid", id));
            }
        });
        if (mOnItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickListener.onItemClick(holder.itemView, pos);
                }
            });
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickListener.onItemLongClick(holder.itemView, pos);
                    return false;
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mDataList == null ? 0 : mDataList.size();
    }

    class MyTVHolder extends RecyclerView.ViewHolder {
        TextView tv_chenggong, tv_obu, tv_vin, tv_time;
        RoundButton roundButton;
        LinearLayout Revin;

        MyTVHolder(View itemView) {
            super(itemView);
            tv_chenggong = (TextView) itemView.findViewById(R.id.tv_chenggong);
            tv_obu = (TextView) itemView.findViewById(R.id.tv_obu);
            tv_vin = (TextView) itemView.findViewById(R.id.tv_vin);
            tv_time = (TextView) itemView.findViewById(R.id.tv_time);
            Revin = (LinearLayout) itemView.findViewById(R.id.Revin);


            roundButton = (RoundButton) itemView.findViewById(R.id.xiangqing);

        }
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);

        void onItemLongClick(View view, int position);
    }

    private MyRVAdapter.OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(MyRVAdapter.OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }
}
